package com.ruoyi.web.controller.invoice;


import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.exception.InvoiceBusinessException;
import com.ruoyi.third.base.InvoiceApiResult;
import com.ruoyi.third.entity.request.BlueTicket;
import com.ruoyi.third.entity.request.DppToken;
import com.ruoyi.third.entity.request.FaceImg;
import com.ruoyi.third.service.ThirdApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Api(tags = "全电")
@RestController
@AllArgsConstructor
@RequestMapping("invoice/qd")
public class QdInvoiceController {


    private ThirdApiService thridApiService;

    @ApiOperation(value = "获取登录验证码接口", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "DppToken", value = "DppToken", dataTypeClass = DppToken.class)
    })
    @PostMapping("getDppToken")
    public InvoiceApiResult getDppToken(@RequestBody @Validated DppToken param) {
        return thridApiService.getDppToken(param);
    }

    @ApiOperation(value = "人脸二维码", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "FaceImg", value = "FaceImg", dataTypeClass = FaceImg.class)
    })
    @PostMapping("getFaceImg")
    public InvoiceApiResult getFaceImg(@RequestBody @Validated FaceImg param) {
        return thridApiService.getFaceImg(param);
    }

    @ApiOperation(value = "获取是否需要人脸", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "nsrsbh", value = "纳税人识别号", dataTypeClass = String.class)
    })
    @PostMapping("queryFaceAuthState")
    public InvoiceApiResult queryFaceAuthState(@RequestBody Map<String, String> param) {
        if (StrUtil.isBlank(param.get("nsrsbh")) ) {
            InvoiceBusinessException.createAndThrowIt("参数错误");
        }
        return thridApiService.queryFaceAuthState(param.get("nsrsbh"));
    }

    @ApiOperation(value = "全电蓝票开具接口", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "BlueTicket", value = "BlueTicket", dataTypeClass = BlueTicket.class)
    })
    @PostMapping("blueTicket")
    public InvoiceApiResult blueTicket(@RequestBody @Validated BlueTicket param) {
        InvoiceApiResult<String> result= thridApiService.queryFaceAuthState(param.getXhdwsbh());
        String slStr = result.getData();
        String deCodeSlStr = Base64.decodeStr(slStr);
       JSONObject  obj= JSONUtil.parseObj(deCodeSlStr);
       String sfsl = obj.getStr("Sfsl");
//       if ("Y".equals(sfsl)){
//           FaceImg faceImg = new FaceImg();
//           faceImg.setNsrsbh(param.getXhdwsbh());
////           faceImg.setType();
//          InvoiceApiResult faceImgRes =  thridApiService.getFaceImg(faceImg);
//          faceImgRes.setCode(201);
//          return faceImgRes;
//       }
        return thridApiService.blueTicket(param);
    }


    @ApiOperation(value = "发票详细查询", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "nsrsbh", value = "纳税人识别号", dataTypeClass = String.class),
            @ApiImplicitParam(name = "fpqqlsh", value = "发票请求流水号", dataTypeClass = String.class),
            @ApiImplicitParam(name = "fpdm", value = "发票代码", dataTypeClass = String.class)
    })
    @PostMapping("qdInvoiceQueryMx")
    public InvoiceApiResult qdinvoiceQueryMx(@RequestBody Map<String, String> param) {
        if (StrUtil.isBlank(param.get("nsrsbh")) || StrUtil.isBlank(param.get("fpqqlsh")) ||StrUtil.isBlank(param.get("fpdm")) ) {
            InvoiceBusinessException.createAndThrowIt("参数错误");
        }
        return thridApiService.invoiceQueryMx(param.get("nsrsbh"),param.get("fpqqlsh"),param.get("fpdm"));
    }


    @ApiOperation(value = "授信额度查询接口", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "nsrsbh", value = "纳税人识别号", dataTypeClass = String.class)
    })
    @PostMapping("getCreditLine")
    public InvoiceApiResult qdgetCreditLine(@RequestBody Map<String, String> param) {
        if (StrUtil.isBlank(param.get("nsrsbh")) ) {
            InvoiceBusinessException.createAndThrowIt("参数错误");
        }
        return thridApiService.getCreditLine(param.get("nsrsbh"));
    }




    @ApiOperation(value = "获取版式文件", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fphm", value = "取版式类型1：PDF 2：OFD 3：XML", dataTypeClass = String.class),
            @ApiImplicitParam(name = "downflag", value = "纳税人识别号", dataTypeClass = String.class),
            @ApiImplicitParam(name = "nsrsbh", value = "发票号码", dataTypeClass = String.class),
            @ApiImplicitParam(name = "kprq", value = "开票日期格式：yyyyMMddHHmms", dataTypeClass = String.class),
            @ApiImplicitParam(name = "addSeal", value = "是否添加签章 否 默认不添加1-添加，其余任意值-不添加", dataTypeClass = String.class)
    })
    @PostMapping("getInvoicePdfOfd")
    public InvoiceApiResult qdgetInvoicePdfOfd(@RequestBody Map<String, String> param) {
        if (StrUtil.isBlank(param.get("fphm")) || StrUtil.isBlank(param.get("downflag")) ||StrUtil.isBlank(param.get("nsrsbh"))||StrUtil.isBlank(param.get("kprq"))||StrUtil.isBlank(param.get("addSeal")) ) {
            InvoiceBusinessException.createAndThrowIt("参数错误");
        }
        return thridApiService.getInvoicePdfOfd(param.get("fphm"),param.get("downflag"),param.get("nsrsbh"),param.get("kprq"),param.get("addSeal"));
    }
    @PostMapping("getFaceAuthResult")
    public InvoiceApiResult getFaceAuthResult(@RequestBody FaceImg faceImg) {

        return thridApiService.getFaceAuthResult(faceImg);
    }
}
